

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;


public class UserManager{
	 public static BufferedReader bufread;
	    //指定文件路径和名称z
	    private static String path = "D:/suncity.txt";
	    private static  File filename = new File(path);
	    private static String readStr ="";


	    /** *//**
	     * 创建文本文件.
	     * @throws IOException 
	     * 
	     */
	    public static void creatTxtFile() throws IOException{
	        if (!filename.exists()) {
	            filename.createNewFile();
	            System.err.println(filename + "已创建！");
	        }
	    }
	    
	  
	    /** *//**
	     * 读取文本文件.
	     * 
	     */
	    public static void readTxtFile(String file){
	        String read;
	        FileReader fileread;
	        try {
	            fileread = new FileReader(file);
	            bufread = new BufferedReader(fileread);
	            try {
	                while ((read = bufread.readLine()) != null) {
	                    readStr = readStr + read+ "\r\n";
	                }
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        } catch (FileNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        System.out.println(readStr.replace(";", "\t"));
	        readStr =null;
	    }
	    
	    public static String TxtFile(String file){
	        String read;
	        FileReader fileread;
	        try {
	            fileread = new FileReader(file);
	            bufread = new BufferedReader(fileread);
	            try {
	                while ((read = bufread.readLine()) != null) {
	                    readStr = readStr + read+ "\r\n";
	                }
	            } catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	            }
	        } catch (FileNotFoundException e) {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }

	        return readStr;
	    }
	    
	    /** *//**
	     * 写文件.
	     * 
	     */
	    public static void writeTxtFile(String newStr,String url) throws IOException{
	    	 
	    	String readStr =TxtFile(url);
	        //先读取原有文件内容，然后进行写入操作
	      
	        String filein = newStr + "\r\n" + readStr + "\r\n";
	        RandomAccessFile mm = null;
	        try {
	            mm = new RandomAccessFile(url, "rw");
	            mm.writeBytes(filein);
	            readStr=null;
	            filein=null;
	        } catch (IOException e1) {
	            // TODO 自动生成 catch 块
	            e1.printStackTrace();
	        } finally {
	        	
	            if (mm != null) {
	                try {
	                    mm.close();
	                } catch (IOException e2) {
	                    // TODO 自动生成 catch 块
	                    e2.printStackTrace();
	                }
	            }
	        }
	    }
	    
	    /** *//**
	     * 将文件中指定内容的第一行替换为其它内容.
	     * 
	     * @param oldStr
	     *            查找内容
	     * @param replaceStr
	     *            替换内容
	     */
	    public static void replaceTxtByStr(String oldStr,String replaceStr) {
	        String temp = "";
	        try {
	            File file = new File(path);
	            FileInputStream fis = new FileInputStream(file);
	            InputStreamReader isr = new InputStreamReader(fis);
	            BufferedReader br = new BufferedReader(isr);
	            StringBuffer buf = new StringBuffer();

	            // 保存该行前面的内容
	            for (int j = 1; (temp = br.readLine()) != null
	                    && !temp.equals(oldStr); j++) {
	                buf = buf.append(temp);
	                buf = buf.append(System.getProperty("line.separator"));
	            }

	            // 将内容插入
	            buf = buf.append(replaceStr);

	            // 保存该行后面的内容
	            while ((temp = br.readLine()) != null) {
	                buf = buf.append(System.getProperty("line.separator"));
	                buf = buf.append(temp);
	            }
	            br.close();
	            FileOutputStream fos = new FileOutputStream(file);
	            PrintWriter pw = new PrintWriter(fos);
	            pw.write(buf.toString().toCharArray());
	            pw.flush();
	            pw.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        
	        
	    }
	    
	    public void read(String url)
	    {
	    	  UserManager.readTxtFile(url);
	    }
	//-----------------------------------------------------------------
	// Constructor
	//-----------------------------------------------------------------

	public UserManager(){}
	
	//-----------------------------------------------------------------
	//Check that all the classes are reachable
	// This should be removed from your final program 
	//-----------------------------------------------------------------
	public void call() {
		UserList reached1 = new UserList();
		ListSort reached2 = new ListSort();
		System.out.println("\nuserList reached by userManager? "
				+ reached1.userListReached());
		System.out.println("listSort reached by userManager? "
				+ reached2.listSortReached());
	}

	//-----------------------------------------------------------------
	// Method for testing that class has been reached
	// This should be removed from your final program 		
	//-----------------------------------------------------------------	
	public boolean userManagerReached(){
			call();
			return true;			
	}	
    } // end of userManager class 
//-----------------------------------------------------------------
